package rad.framework.validator;

import org.hibernate.validator.Validator;

/**
 * Base class of the string validators.
 * 
 * @author Chunyun Zhao
 */
public abstract class AbstractStringValidator implements Validator {
	public boolean isValid(Object value) {
		if ( isEmpty(value) ) {
			return true;
		}
		
		if ( !(value instanceof String)) {
			return false;
		}
		
		return validate((String)value);
	}
	
	private boolean isEmpty(Object value) {
		if ( value == null ) {
			return true;
		}

		if ( value instanceof String && ((String)value).trim().length() == 0 ) {
			return true;
		}

		return false;
	}
	
	protected abstract boolean validate(String value);
}
